package top.infopub.mgr.cap.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.cap.RepaymentScheduleDto;
import top.infopub.enums.cap.CapProcessDetailTypeEnum;
import top.infopub.mgr.cap.dao.RepaymentScheduleDao;
import top.infopub.mgr.cap.service.CapProcessManageService;
import top.infopub.mgr.cap.service.RepaymentScheduleService;
import top.infopub.mgr.cap.vo.RepaymentScheduleInfoVo;
import top.infopub.mgr.cap.vo.RepaymentScheduleVo;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.model.cap.RepaymentSchedule;
import top.infopub.model.cap.RepaymentScheduleSublist;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import java.util.Date;
import java.util.List;

/**
 * @Author Warking
 * @Date 2021/5/24 10:33
 * @Version 1.0  还款计划
 */
@Service
public class RepaymentScheduleServiceImpl implements RepaymentScheduleService {

    @Autowired
    private RepaymentScheduleDao repaymentScheduleDao;

    @Autowired
    private CapProcessManageService capProcessManageService;

    /*还款计划列表接口*/
    @Override
    public Page<RepaymentSchedule> selectPage(Page<RepaymentSchedule> page, RepaymentSchedule repaymentSchedule) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return repaymentScheduleDao.selectPageQuery(repaymentSchedule);
    }

    @Override
    public RepaymentSchedule findById(String id) {
        return repaymentScheduleDao.findById(id);
    }

    @Override
    public RepaymentScheduleDto queryDetail(String id) {
        return null;
    }

    @Override
    public RspVo queryDetailByRepay(String id) {
        RepaymentScheduleVo scheduleVo = new RepaymentScheduleVo();
        RepaymentScheduleInfoVo vo = repaymentScheduleDao.queryDetailByRepay(id);
        //查询子表信息
        List<RepaymentScheduleSublist> scheduleSublists = repaymentScheduleDao.selectSublist(id);
        scheduleVo.setInfoVo(vo);
        scheduleVo.setSublists(scheduleSublists);
        return RspVo.getSuccessResponse(scheduleVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RspVo saveAll(AuthorizationVO userDtl, String bodyStr, Integer submitType, RepaymentSchedule repaymentSchedule, List<RepaymentScheduleSublist> sublists) {
        //处理主表数据
        if (StringUtils.isNotBlank(repaymentSchedule.getId())) {
            repaymentScheduleDao.updateRep(repaymentSchedule);
        } else {
            repaymentSchedule.setId(IdFactory.getUUIDUpper());
            repaymentScheduleDao.insertRep(repaymentSchedule);
        }
        //处理子表数据
        repaymentScheduleDao.deleteSublist(repaymentSchedule.getId());
        for (RepaymentScheduleSublist sublist : sublists) {
            sublist.setAddBy(userDtl.getId());
            sublist.setAddTime(new Date());
            sublist.setRepaymentScheduleId(repaymentSchedule.getId());
        }
        if (sublists.size() > 0) {
            repaymentScheduleDao.insertBatchSublist(sublists);
        }
        if (submitType == 2) {
            //发起流程
            String process = capProcessManageService.sendProcess(CapProcessDetailTypeEnum.PRO_NODE_TYPE14, bodyStr, repaymentSchedule.getId(), repaymentSchedule.getProCode(), userDtl);
        }


        return submitType == 1 ? RspVo.getSuccessResponse(200, "保存成功") : RspVo.getSuccessResponse(200, "提交成功");
    }

}
